﻿// ==============================================================================
// Copyright (c) 2012 Bndy.Net, All rights reserved.
// Created by Bndy on 4/19/2012 9:10:44 PM
//
//                    * * * * * * * * * * * * * 
//                    * MAIL- bndy5337@163.com *       http://www.bndy.net
//                    * Q Q - 81795705 =========================================>
//                    * MSN - bndy5337@msn.com *
//                    * * * * * * * * * * * * * 
//
// ------------------------------------------------------------------------------
// 
// ==============================================================================

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Net.Bndy.Framework.CmdBuilders
{
    public class MsSqlCmdBuilder : CmdBuilder
    {
        public override IDbCommand BuildCommand(EntityBase entity, CommandType cmdType)
        {
            IDbCommand cmd = new SqlCommand();

            Dictionary<string, object> propertiesWithoutPrimaryKey = entity.GetPropertyValuePairWithoutPrimaryKey();
            KeyValuePair<string, object>? primaryKey = entity.GetPrimaryKey();

            List<string> parameters = propertiesWithoutPrimaryKey.Keys.ToList();
            List<string> args = new List<string>();
            string sqlFormat = null;

            switch (cmdType)
            { 
                case CommandType.Delete:
                    sqlFormat = "DELETE FROM [{0}] WHERE {1}";
                    args.Add(entity.GetType().Name);
                    args.Add(string.Format("{0}=@{0}", primaryKey.Value.Key));
                    parameters.Add(primaryKey.Value.Key);
                    break;

                case CommandType.Insert:
                    sqlFormat = "INSERT INTO [{0}]({1}) VALUES({2})";
                    args.Add(entity.GetType().Name);
                    args.Add(string.Join(",", parameters));
                    args.Add("@" + string.Join(",@", parameters));
                    break;

                case CommandType.Update:
                    sqlFormat = "UPDATE [{0}] SET {1} WHERE {2}";
                    args.Add(entity.GetType().Name);
                    List<string> tmp = new List<string> ();
                    foreach(string str in parameters){
                        tmp.Add(string.Format("{0}=@{0}",str));
                    }
                    args.Add(string.Join(",", tmp.ToArray()));
                    args.Add(string.Format("{0}=@{0}", primaryKey.Value.Key));
                    break;
            }

            cmd.CommandText = string.Format(sqlFormat, args);

            System.Data.IDataParameter param; 
            foreach (string field in parameters)
            {
                param = new SqlParameter("@" + field, propertiesWithoutPrimaryKey[field]);
                cmd.Parameters.Add(param);
            }

            return cmd;
        }
    }
}
